<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9; IE=10" />
  <meta name="keywords" content="addSidebarControl,addGeoJSONContainer,getGeoJSON,onGeoJSONParsed,getCustomPointPresentation" />
  <title>HERE Maps API Example: Loading styled geoJSON Points using AJAX: Premiership Teams</title>
  <!-- Set up constants such as APP ID and token -->
  <script type="text/javascript" src="../libs/hereAppIdAndToken.js"></script>
  <!-- Bootstrap jQuery Library -->
  <script type="text/javascript" src="../libs/jQl.min.js"></script>

  <!-- Asynchronously the HERE Maps API for JavaScript -->
  <script type="text/javascript" src="../libs/hereAsyncLoader.js"
    id="HereMapsLoaderScript"
    data-params="maps,datarendering"
    data-map-container="mapContainer"
    data-callback="afterHereMapLoad"
    data-libs="sidebar-control,geojson-parser"
    data-parent="demos/geojson-parser/">
  </script>



  <link rel="icon" href="http://here.com/favicon.ico"/>
  <!--<link href="http://developer.here.com/html/css/main.css" rel="stylesheet" />-->
</head>
<body>
  <h1>Loading styled geoJSON Points using AJAX: Premiership Teams</h1>
  <p>This retrieves and parses geoJSON data holding the names and locations of football stadiums in England.
    Each entry in the data set specifies either the color of the marker or a custom icon. The Points are
    processed using a custom theme.
  </p>
  <p>The JSON file is located at:
    <a href="data/premiership.geojson">data/premiership.geojson</a>
  </p>
  <div id="mapContainer" style="width:540px; height:334px;float:left;"></div>
  <div style="float:left; color: rgb(102, 102, 102);">
    <strong>Premiership Teams</strong>
    <div id="sidebar" style="height:300px; overflow:auto;"></div>
  </div>
  <div id="src" style="clear:both;width:100%">
    <br/>
      <p>The parser can be loaded using the following <code>&lt;script&gt;</code> element:
      <pre><code class='prettyprint'>&lt;script type="text/javascript" charset="UTF-8" src="<a href="libs/geojson-parser.js">libs/geojson-parser.js</a>"&gt;&lt;/script></code></pre>
      <p>The sidebar can be loaded using the following <code>&lt;script&gt;</code> element:
      <pre><code class='prettyprint'>&lt;script type="text/javascript" charset="UTF-8" src="<a href="libs/sidebar-control.js">libs/sidebar-control.js</a>"&gt;&lt;/script></code></pre>
      <br/>
  </div>
<script id="example-code" data-categories="geojson,library" type="text/javascript" >
//<![CDATA[
var map,
  infoBubbles,
  jsonManager,
  resultSet,
  container;


function onGeoJSONParsed() {

  if (jsonManager.state === 'finished') {
    var err = resultSet.addGeoJSON(jsonManager.object);
    if (resultSet.state === 'finished') {
      map.zoomTo(container.getBoundingBox());
      var TOUCH = nokia.maps.dom.Page.browser.touch,
        CLICK = TOUCH ? 'tap' : 'click';
      container.addListener(CLICK, function (evt) {
        infoBubbles.openBubble(evt.target.properties.Description,
          evt.target.getBoundingBox().getCenter());
      }, false);
    } else {
      alert(err);
    }
  }
}

function getGeoJSON() {
  var filename = 'data/premiership.geojson';
  jsonManager = new JSONManager();
  jsonManager.parseJSON(filename);
  jsonManager.addObserver('state', onGeoJSONParsed);
}


function getCustomPointPresentation(dataPoint, props) {
  var anchorPoint;
  if (props.Icon !== undefined) {
    anchorPoint = new nokia.maps.util.Point(16, 32);
    return new nokia.maps.map.Marker(dataPoint, {
      icon: props.Icon,
      anchor: anchorPoint
    });
  }
  if (props.Color !== undefined) {
    return new nokia.maps.map.StandardMarker(dataPoint, {
      brush: {color: props.Color}
    });
  }
  return GeoJSONTheme.getPointPresentation(dataPoint, props);
}


function addGeoJSONContainer() {
  resultSet = new GeoJSONContainer({
    container: container,
    theme : {
      getPointPresentation :  function (dataPoint, properties) {
        return getCustomPointPresentation(dataPoint, properties);
      }
    }
  });

}



function addSidebarControl(map) {

  container = new Sidebar(document.getElementById('sidebar'),
    {title : 'properties.Name'});// This indicates that the summary is held in a property called 'Name'
  map.objects.add(container);

}

function afterHereMapLoad(theMap) {
  map = theMap;
  infoBubbles = new nokia.maps.map.component.InfoBubbles();
  map.components.add(infoBubbles);

  addSidebarControl(map);
  addGeoJSONContainer();
  getGeoJSON();
}
//]]>
</script>
<script type="text/javascript" src="../libs/prettyprint.js"></script>
</body>
</html>
